<?php


namespace app\api\library\user\updateUserPhone;


use app\api\model\MallUser;
use think\facade\Db;

class V
{
    public static function run($param)
    {
        //查询有无openid
        $userId = _checkEmptyParam($param[ 'userId' ], '用户不能为空');
        $mobile = _checkEmptyParam($param[ 'mobile' ], '昵称不能为空');
        $code   = _checkEmptyParam($param[ 'code' ], '验证码不能为空');
        //查询用户最新的一次验证码
        $userCode = Db::name('mall_mobile_msg')->where([['mobile', '=', $mobile], ['code', '=', $code]])->find();
        if (empty($userCode)) {
            _outPut(204, '', '验证码错误');
        }
        if (false === MallUser::update(['mobile' => $mobile], ['id' => $userId])) {
            _outPut(204, '', '修改失败');
        }
        $userInfo = MallUser::where(['id' => $param[ 'userId' ]])->find();
        $res      = [
            'id'         => $userInfo[ 'id' ],
            'avatar_url' => $userInfo[ 'avatar_url' ],
            'name'       => $userInfo[ 'name' ],
            'mobile'     => $userInfo[ 'mobile' ],
            'balance'    => getPrice($userInfo[ 'balance' ]),
        ];
        _outPut(200, $res);
    }
}